package leetcode;

public class SearchMedian {

    public static void main(String[] args) {
        int arr[] = {1,2};
        int arr1[] = {3, 4};
        SearchMedian sortArrray = new SearchMedian();
        System.out.println(sortArrray.searchMedian(arr, arr1));

    }

    public double searchMedian(int[] nums1, int[] nums2){
        int[] ints = mergeDouble(nums1, nums2);
        double res = 0.0;
        if(ints.length % 2 == 0){
            res = (double)(ints[(ints.length / 2) - 1] +ints[(ints.length / 2)]) / 2;
        }else{
            res = (double)ints[ints.length / 2];
        }
        return res;
    }



    public int[] mergeDouble(int[] nums1, int[] nums2) {
        int p1 = 0, p2 = 0, m = nums1.length, n = nums2.length;
        int arr[] = new int[m + n];
        int cur = 0;
        while(p1 < m || p2 < n){
            if(p1 == m){
                cur = nums2[p2 ++];
            }else if(p2 == n){
                cur = nums1[p1 ++];
            }else if(nums1[p1] < nums2[p2]){
                cur = nums1[p1 ++];
            }else {
                cur = nums2[p2 ++];
            }
            arr[ p1 + p2 - 1] = cur;
        }
        return arr;
    }

}
